package com.fucai.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fucai.controller.vo.Fucai3dDingweiDTO;
import com.fucai.entity.Fucai3d;
import com.fucai.entity.Fucai3dDingwei;
import com.fucai.mapper.Fucai3dDingweiMapper;
import com.fucai.mapper.Fucai3dMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @description: 每日定位业务层处理
 * @author: zhuchi
 * @create: 2025-06-21 13:47
 * @Version 1.0
 **/
@Service
public class Fucai3dDingweiService extends ServiceImpl<Fucai3dDingweiMapper, Fucai3dDingwei> {

    @Resource
    private Fucai3dService fucai3dService;

    @Resource
    private Fucai3dMapper fucai3dMapper;

    public Boolean addOrEditDingwei(Fucai3dDingwei reqVO){
        LambdaQueryWrapper<Fucai3dDingwei> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Fucai3dDingwei::getUserId, reqVO.getUserId());
        lambdaQueryWrapper.eq(Fucai3dDingwei::getDingweiQihao, reqVO.getDingweiQihao());

        Fucai3dDingwei dingwei = this.baseMapper.selectOne(lambdaQueryWrapper);

        Boolean hava = true;
        if (null == dingwei){
            hava = false;
            dingwei = new Fucai3dDingwei();
        }
        BeanUtils.copyProperties(reqVO, dingwei, "id");

        List<Integer> baiwei = JSONObject.parseArray(reqVO.getBaiwei(), Integer.class);
        List<Integer> shiwei = JSONObject.parseArray(reqVO.getShiwei(), Integer.class);
        List<Integer> gewei = JSONObject.parseArray(reqVO.getGewei(), Integer.class);
        List<String> groups = new ArrayList<>();
        for (Integer bai : baiwei) {
            for (Integer shi : shiwei) {
                for (Integer ge : gewei) {
                    groups.add(String.valueOf(bai) + String.valueOf(shi) + String.valueOf(ge));
                }
            }
        }
        // 计算购买金额
        Integer purchaseAmount = groups.size() * 2;
        dingwei.setPurchaseAmount(purchaseAmount);

        if (!hava){
            dingwei.setCreateTime(new Date());
            this.baseMapper.insert(dingwei);
        }else {
            this.baseMapper.updateById(dingwei);
        }

        return true;
    }

    /**
     * @description: 查询定位信息
     * @param: fucai3dDingwei
     * @return: java.util.List<com.fucai.entity.Fucai3dDingwei>
     **/
    public List<Fucai3dDingweiDTO> selectDingweiByQihao(Fucai3dDingwei reqVO) {
        List<Fucai3dDingweiDTO> result = this.baseMapper.selectDingweiByQihao(reqVO);
        for (Fucai3dDingweiDTO fucai3dDingweiDTO : result) {
            fucai3dDingweiDTO.setDingweiResult("不正确");
            if (fucai3dDingweiDTO.getBaiwei().contains(String.valueOf(fucai3dDingweiDTO.getZjB()))){
                if (fucai3dDingweiDTO.getShiwei().contains(String.valueOf(fucai3dDingweiDTO.getZjS()))){
                    if (fucai3dDingweiDTO.getGewei().contains(String.valueOf(fucai3dDingweiDTO.getZjG()))){
                        fucai3dDingweiDTO.setDingweiResult("正确");
                    }
                }
            }
        }
        return result;
    }
    
    public void assignDingwei() {
        for (int i = 121; i < 397; i++) {
            this.addDingweiByHistory(i);
        }
    }

    /**
     * @description: 根据历史添加定位信息
     * @param: fucai3dDingwei
     * @return: java.lang.Boolean
     **/
    public Boolean addDingweiByHistory(Integer id) {
        Fucai3d newData = fucai3dService.getNewData();
        if (null != id){
            newData = fucai3dService.getById(id);
        }
        Integer maxId = newData.getId();
        Integer minId = maxId - 120;

        Integer zjB = newData.getZjB();
        Integer zjS = newData.getZjS();
        Integer zjG = newData.getZjG();

        List<String> historyDingweiBaiwei = new ArrayList<>();
        List<String> historyDingweiShiwei = new ArrayList<>();
        List<String> historyDingweiGewei = new ArrayList<>();

        List<Fucai3d> fucai3dList = fucai3dMapper.getFucai3dListByStartIdAndEndId(minId, maxId);
        Fucai3d fucai3d = null;
        for (int i = 0; i < fucai3dList.size(); i++) {
            fucai3d = fucai3dList.get(i);
            if (fucai3d.getZjB().equals(zjB)){
                if ((i + 1) < fucai3dList.size()){
                    String baiwei = String.valueOf(fucai3dList.get(i + 1).getZjB());
                    if (!historyDingweiBaiwei.contains(baiwei)){
                        historyDingweiBaiwei.add(baiwei);
                    }
                }
            }
            if (fucai3d.getZjS().equals(zjS)){
                if ((i + 1) < fucai3dList.size()){
                    String shiwei = String.valueOf(fucai3dList.get(i + 1).getZjS());
                    if (!historyDingweiShiwei.contains(shiwei)){
                        historyDingweiShiwei.add(shiwei);
                    }
                }
            }
            if (fucai3d.getZjG().equals(zjG)){
                if ((i + 1) < fucai3dList.size()){
                    String gewei = String.valueOf(fucai3dList.get(i + 1).getZjG());
                    if (!historyDingweiGewei.contains(gewei)){
                        historyDingweiGewei.add(gewei);
                    }
                }
            }
        }
        Collections.sort(historyDingweiBaiwei);
        Collections.sort(historyDingweiShiwei);
        Collections.sort(historyDingweiGewei);

        System.out.println(historyDingweiBaiwei);
        System.out.println(historyDingweiShiwei);
        System.out.println(historyDingweiGewei);

        String qihao = String.valueOf(newData.getQihao()).substring(0, 4);
        String qihao2 = String.valueOf(newData.getQihao()).substring(4, 7);
        Integer qihao3 = Integer.valueOf(qihao2) + 1;
        qihao2 = StringUtils.leftPad(String.valueOf(qihao3), 3, "0");

        qihao += qihao2;
        Fucai3dDingwei reqVO = new Fucai3dDingwei();
        reqVO.setDingweiQihao(qihao);
        reqVO.setUserId("200");
        reqVO.setUserName("历史120期");
        reqVO.setBaiwei(JSONObject.toJSONString(historyDingweiBaiwei));
        reqVO.setShiwei(JSONObject.toJSONString(historyDingweiShiwei));
        reqVO.setGewei(JSONObject.toJSONString(historyDingweiGewei));
        this.addOrEditDingwei(reqVO);

        return true;
    }

    public void  update(){
        LambdaQueryWrapper<Fucai3dDingwei> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Fucai3dDingwei> list = this.baseMapper.selectList(lambdaQueryWrapper);
        for (Fucai3dDingwei fucai3dDingwei : list) {
            List<Integer> baiwei = JSONObject.parseArray(fucai3dDingwei.getBaiwei(), Integer.class);
            List<Integer> shiwei = JSONObject.parseArray(fucai3dDingwei.getShiwei(), Integer.class);
            List<Integer> gewei = JSONObject.parseArray(fucai3dDingwei.getGewei(), Integer.class);
            List<String> groups = new ArrayList<>();
            for (Integer bai : baiwei) {
                for (Integer shi : shiwei) {
                    for (Integer ge : gewei) {
                        groups.add(String.valueOf(bai) + String.valueOf(shi) + String.valueOf(ge));
                    }
                }
            }
            // 计算购买金额
            Integer purchaseAmount = groups.size() * 2;
            fucai3dDingwei.setPurchaseAmount(purchaseAmount);
            this.updateById(fucai3dDingwei);
        }
    }
}
